package com.lin.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.demo.annotations.CacheLock;
import com.lin.demo.annotations.LocalLock;
import com.lin.demo.annotations.Resubmit;
import com.lin.demo.entity.User;
import com.lin.demo.mapper.UserMapper;
import com.lin.demo.model.R;
import com.lin.demo.service.UserService;
import com.lin.demo.service.async.RegisterUserService;
import com.lin.demo.vo.UserByAgeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author zimingl
 * @Date 2022/9/25 23:43
 * @Description: TODO
 */
@Api(tags = "UserController")
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private UserMapper userMapper;

    @Resource
    private RegisterUserService registerUserService;

    @GetMapping("/getList")
    public List<Map<String, Object>> getList() {

        // 方法一
        List<User> list1 = userService.lambdaQuery().eq(User::getAge, "1").list();
        List<User> list2 = userService.list();

        Page<User> page = new Page<>(1, 1);
        userMapper.selectPage(page, null);
        // 方法二
        List<User> users = userMapper.selectList(null);
        //获取分页数据
        List<User> list = page.getRecords();
        List<Map<String, Object>> res = new ArrayList<>();
        Map map = new HashMap();
        map.put("pageNum", page.getPages());
        map.put("pageSize", page.getSize());
        map.put("current", page.getCurrent());
        map.put("total", page.getTotal());
        map.put("data", users);
        res.add(map);
        return res;
    }

    @GetMapping("/getList2")
    public R getList2(@RequestParam("pageSize") Integer pageSize, @RequestParam("currentPageNum") Integer currentPageNum) {
        if (pageSize == null) {
            pageSize = 10;
        }
        if (currentPageNum == null) {
            currentPageNum = 1;
        }
        Page<User> page = new Page<>(currentPageNum, pageSize);
        userMapper.selectPage(page, null);
        return R.ok().data(page.getRecords(), page.getTotal());
    }

    @GetMapping("/getListByAge")
    public Map<String, Object> getListByAge(@RequestBody UserByAgeVo userByAgeVo) {
        if (userByAgeVo.getPageSize() == null) {
            userByAgeVo.setPageSize(10);
        }
        if (userByAgeVo.getCurrentPageNum() == null) {
            userByAgeVo.setCurrentPageNum(1);
        }
        if (userByAgeVo.getAge() == null) {
            userByAgeVo.setAge(0);
        }
        Page<User> page = new Page<>(userByAgeVo.getCurrentPageNum(), userByAgeVo.getPageSize());
        userMapper.selectPageVo(page, userByAgeVo.getAge());
        Map<String, Object> map = getResult(page);
        return map;
    }

    private <T> Map<String, Object> getResult(Page<T> page) {
        List<T> list = page.getRecords();
        Map map = new HashMap();
        map.put("pageNum", page.getPages());
        map.put("pageSize", list.size() > page.getSize() ? page.getSize() : list.size());
        map.put("current", page.getCurrent());
        map.put("total", page.getTotal());
        map.put("data", list);
        return map;
    }

    // 23,24
    // ybc3
    @GetMapping("/getListByNameAndAges")
    public List<User> getListByNameAndAges(@RequestParam("name") String name, @RequestParam("ages") List<String> ages) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getName, name);
        wrapper.in(User::getAge, ages);
        return userService.list(wrapper);
    }

    @PostMapping("/add")
    @Resubmit
    @ApiOperation(value = "jdk 本地锁 测试")
    public R add(@RequestBody User user) {
        userService.save(user);
        return R.ok().data("data", user);
    }

    @PostMapping("/add2")
    @LocalLock(delaySeconds = 1000, key = "localLock:add2:arg[0]")
    @ApiOperation(value = "guava 本地锁 测试")
    public R add2(@RequestBody User user) {
        userService.save(user);
        return R.ok().data("data", user);
    }

    @PostMapping("/add3")
    @CacheLock(expire = 1000, prefix = "CacheLock:add3:arg[0]")
    @ApiOperation(value = "redis 锁 测试")
    public R add3(@RequestBody User user) {
        userService.save(user);
        return R.ok().data("data", user);
    }

    @PostMapping("/register")
    @ApiOperation(value = "register 送积分")
    public R register() {
        boolean register = registerUserService.register("138******90", "386493");
        return R.ok().success(register);
    }
}
